<?php
//ten san pham
$sql = "SELECT * FROM #__categories WHERE published=1 AND parent_id!=0 AND section=1 ORDER BY parent_id ASC, id DESC";
$database->setQuery($sql);
$lsNameProducts = $database->loadObjectList();
//hang sx
$sql = "SELECT * FROM #__catphone WHERE published=1 ORDER BY ordering ASC, id DESC";
$database->setQuery($sql);
$lsHangsx = $database->loadObjectList(); 
//Khung gia 300k
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<300000 AND price>0) OR (price_giam<300000 AND price_giam>0))";
$database->setQuery($sql);
$total_300k = $database->loadResult();
//Khung gia 300-500k
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<500000 AND price>=300000) OR (price_giam<500000 AND price_giam>=300000))";
$database->setQuery($sql);
$total_300_500k = $database->loadResult();
//Khung gia 500k-1MB
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<1000000 AND price>=500000) OR (price_giam<1000000 AND price_giam>=500000))";
$database->setQuery($sql);
$total_500k_1MB = $database->loadResult();
//Khung gia 1MB-3MB
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<3000000 AND price>=1000000) OR (price_giam<3000000 AND price_giam>=1000000))";
$database->setQuery($sql);
$total_1MB_3MB = $database->loadResult();
//Khung gia 3MB-5MB
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<5000000 AND price>=3000000) OR (price_giam<5000000 AND price_giam>=3000000))";
$database->setQuery($sql);
$total_3MB_5MB = $database->loadResult();
//Khung gia 5MB-10MB
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND ((price<10000000 AND price>=5000000) OR (price_giam<10000000 AND price_giam>=5000000))";
$database->setQuery($sql);
$total_5MB_10MB = $database->loadResult();
//Khung gia >10MB
$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND (price>10000000 OR price_giam>10000000)";
$database->setQuery($sql);
$total_10MB = $database->loadResult();
?>
<div class="box" id="search_advance">
	<div class="title">
		<div class="left"></div>
		<div class="center">Tìm theo tiêu chí</div>
		<div class="right"></div>
	</div>
	<div class="boxContent">
		<div class="title_search_advande"><span>Tên sản phẩm</span></div>
		<ul>
		<?php
		for ($i=0; $i<count($lsNameProducts); $i++){
			//$link = sefRelToAbs("index.php?option=com_content&task=blogcategory&id=".$lsNameProducts[$i]->id."-".$lsNameProducts[$i]->name);
			$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND catid=".$lsNameProducts[$i]->id;
			$database->setQuery($sql);
			$total = $database->loadResult();
			if($total>0){	 
			?>
			<li class="lis search" id="searchnamesp_<?php echo $lsNameProducts[$i]->id;?>"><a id="namesp_<?php echo $lsNameProducts[$i]->id;?>"><?php echo $lsNameProducts[$i]->title;?></a> (<?php echo $total;?>)</li>
			<?php 
			}
		}	
		?>
		</ul>
		<div class="title_search_advande"><span>Khung giá</span></div>
		<ul>
			<?php if($total_300k>0){?>
			<li class="search" id="searchframe_D300k"><a id="frame_D300k">Dưới 300 ngàn</a> (<?php echo $total_300k;?>)</li>
			<?php }?>
			<?php if($total_300_500k>0){?>
			<li class="search" id="searchframe_300k_500k"><a id="frame_300k_500k">300 ngàn - 500 ngàn</a> (<?php echo $total_300_500k;?>)</li>
			<?php }?>
			<?php if($total_500k_1MB>0){?>
			<li class="search" id="searchframe_500k_1MB"><a id="frame_500k_1MB">500 ngàn - 1 triệu</a> (<?php echo $total_500k_1MB;?>)</li>
			<?php }?>
			<?php if($total_1MB_3MB>0){?>
			<li class="search" id="searchframe_1MB_3MB"><a id="frame_1MB_3MB">1 - 3 triệu</a> (<?php echo $total_1MB_3MB;?>)</li>
			<?php }?>
			<?php if($total_3MB_5MB>0){?>
			<li class="search" id="searchframe_3MB_5MB"><a id="frame_3MB_5MB">3 - 5 triệu</a> (<?php echo $total_3MB_5MB;?>)</li>
			<?php }?>
			<?php if($total_5MB_10MB>0){?>
			<li class="search" id="searchframe_5MB_10MB"><a id="frame_5MB_10MB">5 - 10 triệu</a> (<?php echo $total_5MB_10MB;?>)</li>
			<?php }?>
			<?php if($total_10MB>0){?>
			<li class="search" id="searchframe_H10MB"><a id="frame_H10MB">Lớn hơn 10 triệu</a> (<?php echo $total_10MB;?>)</li>
			<?php }?>
		</ul>
		<div class="title_search_advande"><span>Hãng sản xuất</span></div>
		<ul>
		<?php
		for ($i=0; $i<count($lsHangsx); $i++){
			$link = sefRelToAbs("index.php?option=com_content&task=hangsx&id=".$lsHangsx[$i]->id);
			$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND catphone_id=".$lsHangsx[$i]->id;
			$database->setQuery($sql);
			$total = $database->loadResult();
			if($total>0){	 
			?>
			<li class="lihang search" id="searchhangsx_<?php echo $lsHangsx[$i]->id;?>"><a id="hangsx_<?php echo $lsHangsx[$i]->id;?>"><?php echo $lsHangsx[$i]->name;?></a> (<?php echo $total;?>)</li>
			<?php 
			}
		}	
		?>
		</ul>
	</div>
</div>
<script type="text/javascript">
	$(function(){
		var url = '<?php echo $mosConfig_live_site;?>/ajax/search_advance.php?check=1';
		//Ten san pham
		<?php
		for ($i=0; $i<count($lsNameProducts); $i++){ 
		?>
		$('#namesp_<?php echo $lsNameProducts[$i]->id;?>').click(function (){
			if($('#searchnamesp_<?php echo $lsNameProducts[$i]->id;?>').hasClass('search')){
				$('#searchnamesp_<?php echo $lsNameProducts[$i]->id;?>').addClass('searchCheck').removeClass('search');
				$('#searchnamesp_<?php echo $lsNameProducts[$i]->id;?>').removeClass('lis');
				$('.lis').css('display','none');
				url = url + '&nameID=<?php echo $lsNameProducts[$i]->id;?>';
			}else{
				$('#searchnamesp_<?php echo $lsNameProducts[$i]->id;?>').addClass('lis search').removeClass('searchCheck');
				$('.lis').css('display','block');
				url = url + '&nameID=0';
			}
			$('#colsCenter').load(url);	
		})
		<?php }?>
		//Frame price
		$('#frame_D300k').click(function(){
			if($('#searchframe_D300k').hasClass('search')){
				$('#searchframe_D300k').addClass('searchCheck').removeClass('search');
				$('#searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=D300k';
			}else{
				$('#searchframe_D300k').addClass('search').removeClass('searchCheck');
				$('#searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_300k_500k').click(function(){
			if($('#searchframe_300k_500k').hasClass('search')){
				$('#searchframe_300k_500k').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=300k_500k';
			}else{
				$('#searchframe_300k_500k').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_500k_1MB').click(function(){
			if($('#searchframe_500k_1MB').hasClass('search')){
				$('#searchframe_500k_1MB').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=500k_1MB';
			}else{
				$('#searchframe_500k_1MB').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_1MB_3MB').click(function(){
			if($('#searchframe_1MB_3MB').hasClass('search')){
				$('#searchframe_1MB_3MB').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=1MB_3MB';
			}else{
				$('#searchframe_1MB_3MB').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_3MB_5MB').click(function(){
			if($('#searchframe_3MB_5MB').hasClass('search')){
				$('#searchframe_3MB_5MB').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=3MB_5MB';
			}else{
				$('#searchframe_3MB_5MB').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_5MB_10MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_5MB_10MB').click(function(){
			if($('#searchframe_5MB_10MB').hasClass('search')){
				$('#searchframe_5MB_10MB').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_H10MB').css('display','none');
				url = url + '&frame=5MB_10MB';
			}else{
				$('#searchframe_5MB_10MB').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_H10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		$('#frame_H10MB').click(function(){
			if($('#searchframe_H10MB').hasClass('search')){
				$('#searchframe_H10MB').addClass('searchCheck').removeClass('search');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB').css('display','none');
				url = url + '&frame=H10MB';
			}else{
				$('#searchframe_H10MB').addClass('search').removeClass('searchCheck');
				$('#searchframe_D300k, #searchframe_300k_500k, #searchframe_500k_1MB, #searchframe_1MB_3MB, #searchframe_3MB_5MB, #searchframe_5MB_10MB').css('display','block');
				url = url + '&frame=no';
			}
			$('#colsCenter').load(url);		
		});
		//hang sx
		<?php
		for ($i=0; $i<count($lsHangsx); $i++){ 
		?>
		$('li.search #hangsx_<?php echo $lsHangsx[$i]->id;?>').click(function(){
			if($('#searchhangsx_<?php echo $lsHangsx[$i]->id;?>').hasClass('search')){
				$('#searchhangsx_<?php echo $lsHangsx[$i]->id;?>').addClass('searchCheck').removeClass('search');
				$('#searchhangsx_<?php echo $lsHangsx[$i]->id;?>').removeClass('lihang');
				$('.lihang').css('display','none');
				url = url + '&hangID=<?php echo $lsHangsx[$i]->id;?>';
			}else{
				$('#searchhangsx_<?php echo $lsHangsx[$i]->id;?>').addClass('lihang search').removeClass('searchCheck');
				$('.lihang').css('display','block');
				url = url + '&hangID=0';
			}
			$('#colsCenter').load(url);	
		});
		<?php }?>
	});
</script>